import Store from 'electron-store';
import path from 'path';
import {app} from 'electron';
import {LogCategory, logger} from '../../logger';

/**
 * 基础存储服务类
 * 为其他存储服务提供基础功能
 */
export abstract class BaseStoreService<T extends Record<string, any>> {
  protected store: Store<T>;
  protected storeName: string;
  protected moduleName: string;

  /**
   * 构造函数
   * @param storeName 存储名称
   * @param defaults 默认值
   */
  constructor(storeName: string, defaults: T) {
    this.storeName = storeName;
    this.moduleName = `Store.${storeName}`;

    // 获取数据存储路径
    const storePath = path.join(app.getPath('userData'), 'data');
    logger.info(this.moduleName, LogCategory.SYSTEM, `初始化${storeName}数据存储，路径: ${storePath}`);

    try {
      // 创建electron-store实例
      this.store = new Store<T>({
        name: `chromalift-${storeName.toLowerCase()}`,
        cwd: storePath,
        defaults
      });

      logger.info(this.moduleName, LogCategory.SYSTEM, `${storeName}数据存储初始化成功`);
    } catch (error) {
      logger.error(this.moduleName, LogCategory.SYSTEM, `初始化${storeName}数据存储失败`, error);
      throw error;
    }
  }

  /**
   * 获取存储对象
   */
  public getStore(): Store<T> {
    return this.store;
  }

  /**
   * 清理存储数据
   */
  public abstract cleanupStore(): void;
}
